package com.lanswon.qzsmk.controller.inventoryManageController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.annotation.SystemControllerLog;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.exception.InventoryException;
import com.lanswon.qzsmk.model.StorageModeDefinition;
import com.lanswon.qzsmk.service.systemManageService.InventoryManagementService;
import com.lanswon.qzsmk.vo.StorageRegRecVo;

import ch.qos.logback.classic.Logger;

@Controller
@RequestMapping(value = "/inventoryMagOfOutGoingCancel")
public class InventoryMagOfOutGoingCancellationController {
	private final static Logger logger = (Logger) LoggerFactory.getLogger(InventoryMagOfOutGoingCancellationController.class);
	
	@Autowired
	InventoryManagementService ims;
	
	/**
	 * 跳转出库撤销页面
	 * 
	 * @return
	 */
	@RequiresPermissions("inventoryMagOfOutGoingCancel:index")
	@RequestMapping(name = "出库撤销查询画面,inventoryMagOfOutGoingCancel:index",value = "/queryOutgoRevocation", method = RequestMethod.GET)
	@SystemControllerLog(description = "跳转出库撤销页面")
	public String queryOutgoRevocation(HttpServletRequest request) {

		return "OutgoingRevocation";
	}
	
	/**
	 * 出库页面加载
	 * 
	 * @return getDistributionDetail
	 */
	@RequiresPermissions("inventoryMagOfOutGoingCancel:getOutgoRevoction")
	@RequestMapping(name = "出库信息加载,inventoryMagOfOutGoingCancel:getOutgoRevoction",value = "/getOutgoRevoction", method = RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "出库页面加载")
	public Map<String, Object> getOutgoRevoction(HttpServletRequest request, Integer pageNum, Integer numPerPage) {
		if (pageNum == null && numPerPage == null) {
			pageNum = 1;
			numPerPage = 10;
		}

		// 接收前画面穿过来数据
		String storageNo = null;
		String storageNoAsNum = request.getParameter("storageNo");
		if(!"".equals(storageNoAsNum)&& storageNoAsNum!=null){
			storageNo = "KC"+storageNoAsNum;
		}
		String storageMode = request.getParameter("storageModeBySO");
		String storageDateFrom = request.getParameter("storageDateFrom");
		String storageDateTo = request.getParameter("storageDateTo");
		String curBranchNo = request.getParameter("baranchNoByOutCancel");
		String curEmpNo = request.getParameter("empNoByOutCancel");
		if(storageDateFrom!=null && !storageDateFrom.equals("")){
        	String date[]=storageDateFrom.split("-");
    		String dateFlag= date[0]+date[1]+date[2];
        	storageDateFrom=dateFlag;
        	if(storageDateTo!=null && !storageDateTo.equals("")){
        		String dateTo[]=storageDateTo.split("-");
	    		String dateFlag1= dateTo[0]+dateTo[1]+dateTo[2];
	    		storageDateTo=dateFlag1;
        	}
        }else if(storageDateTo!=null && !storageDateTo.equals("")){
        	String date[]=storageDateTo.split("-");
    		String dateFlag= date[0]+date[1]+date[2];
    		storageDateTo=dateFlag;
    		if(storageDateFrom!=null && !storageDateFrom.equals("")){
        		String dateTo[]=storageDateFrom.split("-");
	    		String dateFlag1= dateTo[0]+dateTo[1]+dateTo[2];
	    		storageDateFrom=dateFlag1;
        	}
        }
		// 装有画面信息的map
		Map<String, String> map = new HashMap<String, String>();
		map.put("storageNo", storageNo);
		map.put("storageMode", storageMode);
		map.put("storageDateFrom", storageDateFrom);
		map.put("storageDateTo", storageDateTo);
		map.put("branchNo", curBranchNo);
		map.put("empNo", curEmpNo);
		DwzPage<StorageRegRecVo> sRV = ims.queryOutgoingRevcotion(map, pageNum, numPerPage);
		Map<String, Object> rMap = new HashMap<>();
		rMap.put("sRV", sRV);
		return rMap;
	}
	
	/**
	 * 获取出库撤销信息
	 * 
	 * @param request
	 * @return
	 * @throws InventoryException 
	 * @throws ApplicationException 
	 */
	@RequiresPermissions("inventoryMagOfOutGoingCancel:OutgoRevoction")
	@RequestMapping(name = "出库信息提交,inventoryMagOfOutGoingCancel:OutgoRevoction",value = "/OutgoRevoction", method = RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "获取出库撤销信息")
	public Map<String, Object> OutgoRevoction(HttpServletRequest request, String outgoRecotiontasked) throws InventoryException {
		Map<String, Object> map = new HashMap<>();
		String checkedParam = outgoRecotiontasked;
		String[] checkParamArray = checkedParam.split("#");
		logger.debug("checkedParam==>" + checkedParam);
		ims.incomingCancelSubmit(checkParamArray);

		
		map.put("statusCode", "200");
		map.put("callbackType", "forward");
		map.put("forwardUrl", "inventoryMagOfOutGoingCancel/queryOutgoRevocation");
		map.put("method", "GET");
		return map;
	}
}
